package com.crp.unitalinforbaseplat.app.sys.controller;

import com.crp.unitalinforbaseplat.app.core.Message;
import com.crp.unitalinforbaseplat.app.sys.entity.Permission;
import com.crp.unitalinforbaseplat.app.sys.entity.SelectEntity;
import com.crp.unitalinforbaseplat.app.sys.entity.User;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.PageBean;
import com.crp.unitalinforbaseplat.app.sys.controller.common.BaseController;
import com.crp.unitalinforbaseplat.app.sys.entity.Role;
import com.crp.unitalinforbaseplat.app.sys.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/unitlinforbase/role/")
public class RoleController extends BaseController {
    @Autowired
    RoleService roleService;

    // 返回用户列表视图
    @RequestMapping("index.crp")
    ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("backend/sys/role/list");
        modelAndView.addObject("userName",getUserName());
        modelAndView.addObject("roleName",getRoleName());
        return modelAndView;
    }
    // 返回用户列表视图
    @RequestMapping("update/index.crp")
    ModelAndView updateIndex(Long id) {
        ModelAndView modelAndView = new ModelAndView("backend/sys/role/update");
        if(id!=null) modelAndView.addObject("role",roleService.updateInit(id));
        else modelAndView.addObject("role",new Role());
        modelAndView.addObject("userName",getUserName());
        modelAndView.addObject("roleName",getRoleName());
        return modelAndView;
    }

    @RequestMapping("addUser/index.crp")
    ModelAndView addUserIndex(Long id) {
        ModelAndView modelAndView = new ModelAndView("backend/sys/role/add_user");
        if( id!= null)  modelAndView.addObject("role",roleService.updateInit(id));
        else {
            modelAndView.addObject("role", new Role());
        }
        modelAndView.addObject("userName",getUserName());
        modelAndView.addObject("roleName",getRoleName());
        return modelAndView;
    }

    @RequestMapping("addPermission/index.crp")
    ModelAndView addPermissionIndex(Long id) {
        ModelAndView modelAndView = new ModelAndView("backend/sys/role/add_permission");
        if( id!= null)  modelAndView.addObject("role",roleService.updateInit(id));
        else {
            modelAndView.addObject("role", new Role());
        }
        modelAndView.addObject("userName",getUserName());
        modelAndView.addObject("roleName",getRoleName());
        return modelAndView;
    }

    @RequestMapping("list.crp")
    @ResponseBody
    PageBean<Role> list(Role role){
        PageBean<Role> pageBean = new PageBean<Role>();
        pageBean.setParameter(role);
        return roleService.list(pageBean);
    }


    @RequestMapping("saveOrUpdate.crp")
    @ResponseBody
    Message saveOrUpdate(Role role){
        return roleService.saveOrUpdate(role);
    }

    @RequestMapping("delete.crp")
    @ResponseBody
    Message delete(Long[] id){
        return roleService.delete(id);
    }

    @RequestMapping("listUserInfo.crp")
    @ResponseBody
    public List<SelectEntity> listUserInfo(Long roleId) throws Exception{
        User sysUserInfo = new User();
        sysUserInfo.setRoleIdStr(roleId);
        return roleService.listUserInfo(sysUserInfo);
    }

    @RequestMapping("listPermission.crp")
    @ResponseBody
    public List<SelectEntity> listPermission(Long roleId) throws Exception{
        Permission permission = new Permission();
        permission.setRoleIdStr(roleId);
        return roleService.listPermission(permission);
    }

    @RequestMapping("roleAddUser.crp")
    @ResponseBody
    public Message roleAddUser(Long[] userInfo,Long id) throws Exception {
        return roleService.roleAddUser(userInfo, id);
    }
    @RequestMapping("roleAddPermission.crp")
    @ResponseBody
    public Message roleAddPermission(Long[] permission,Long id) throws Exception {
        return roleService.roleAddPermission(permission, id);
    }
}
